package com.bell.atensettings.settingDetails;

import java.util.ArrayList;

import com.bell.atensettings.R;
import com.bell.atensettings.bean.SleepTimeItem;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SleepTimeSetting extends Activity implements OnItemClickListener
{
    private Context mContext;
    private ImageView bridgeImage;
    private TextView mTitleView;
    private View mSelectView;
    private ListView mListView;
    private ItemAdapter mItemAdapter;
    private ArrayList<SleepTimeItem> mSleepTimeItem;
    private int mScreenTimeout = 60 * 1000;
    private int mIndex = 0;

    private void initTVoutMode()
    {
        mSleepTimeItem = new ArrayList<SleepTimeItem>();
        try
        {
            mScreenTimeout = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
            String[] timeOut = getResources().getStringArray(R.array.screen_timeout_entries);
            String[] values = getResources().getStringArray(R.array.screen_timeout_values);

            for (int index = 0; index < timeOut.length; index++)
            {
                SleepTimeItem timeItem = new SleepTimeItem();

                timeItem.setName(timeOut[index]);
                timeItem.setData(values[index]);
                mSleepTimeItem.add(timeItem);
            }
        }
        catch (SettingNotFoundException exception)
        {
            exception.printStackTrace();
        }
        catch (Exception exception) 
        {
            exception.printStackTrace();
        }
    }
    
    private void initView()
    {
        initTVoutMode();

        mTitleView = (TextView) findViewById(R.id.setting_title);
        mListView = (ListView) findViewById(R.id.listview);
        mItemAdapter = new ItemAdapter(mSleepTimeItem);

        mTitleView.setText(getIntent().getStringExtra("title"));
        mListView.setAdapter(mItemAdapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);

        setContentView(R.layout.public_settings);
        mContext = this;

        initView();
    }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        String timeOut = mSleepTimeItem.get(position).getData();
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, Integer.parseInt(timeOut));
        finish();

        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }

    public class ItemAdapter extends BaseAdapter
    {
        private ArrayList<SleepTimeItem> mItem;

        public ItemAdapter(ArrayList<SleepTimeItem> items)
        {
            this.mItem = items;
        }

        public int getCount()
        {
            return this.mItem.size();
        }

        public Object getItem(int position)
        {
            return this.mItem.get(position);
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            SleepTimeItem dispItem = (SleepTimeItem) this.mItem.get(position);

            if (convertView == null)
                convertView = ((LayoutInflater) mContext.getSystemService("layout_inflater")).inflate(
                        R.layout.list_display_item, null);
            convertView.setBackgroundResource(R.drawable.listnormal);
            ImageView signView = (ImageView) convertView.findViewById(R.id.img_sign);
            TextView tv_prop = (TextView) convertView.findViewById(R.id.tv_data);
            if (dispItem.getData().equals(String.valueOf(mScreenTimeout)))
            {
                bridgeImage = signView;
                signView.setImageResource(R.drawable.choose_icon);
            }
            else
            {
                signView.setImageDrawable(null);
            }
            tv_prop.setText(dispItem.getName());
            convertView.setTag(signView);
            return convertView;
        }
    }

}
